package com.theme.passport.service.theme.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.utils.Query;
import com.theme.passport.dao.theme.WidgeLayoutDao;
import com.theme.passport.entity.theme.WidgeEntity;
import com.theme.passport.entity.theme.WidgeLayoutEntity;
import com.theme.passport.service.theme.WidgeLayoutService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("widgeLayoutService")
public class WidgeLayoutServiceImpl extends ServiceImpl<WidgeLayoutDao, WidgeLayoutEntity> implements WidgeLayoutService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String keyword = (String) params.get("key");
        String widgeId = (String) params.get("widgeId");
        IPage<WidgeLayoutEntity> page = this.page(
                new Query<WidgeLayoutEntity>().getPage(params),
                new QueryWrapper<WidgeLayoutEntity>()
                        .eq(StringUtils.isNotBlank(widgeId), "widge_id", widgeId)
                        .like(StringUtils.isNotBlank(keyword), "widge_name", keyword)
                        .orderByDesc("created")
        );

        return new PageUtils(page);
    }

    @Override
    public List<WidgeLayoutEntity> queryWidgeLayoutList(Long widgeId) {
        Wrapper<WidgeLayoutEntity> queryWrapper = new QueryWrapper<WidgeLayoutEntity>()
                .eq("widge_id", widgeId);
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public void deleteThemeWidgeLayoutByWidgeId(Long widgeId) {
        Wrapper<WidgeLayoutEntity> queryWrapper = new QueryWrapper<WidgeLayoutEntity>()
                .eq("widge_id", widgeId);
        this.baseMapper.delete(queryWrapper);
    }

    @Override
    public List<WidgeEntity> queryWidgeList(Map<String, Object> params) {
        QueryWrapper<WidgeLayoutEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("DISTINCT widge_id,widge_name").orderByAsc("widge_name");
        List<WidgeLayoutEntity> widgeLayoutList = this.baseMapper.selectList(queryWrapper);
        if (widgeLayoutList != null && widgeLayoutList.size() > 0){
            List<WidgeEntity> widgeList = new ArrayList<>();
            widgeLayoutList.forEach(widgeLayoutEntity ->{
                WidgeEntity widgeEntity = new WidgeEntity();
                widgeEntity.setId(widgeLayoutEntity.getWidgeId());
                widgeEntity.setName(widgeLayoutEntity.getWidgeName());
                widgeList.add(widgeEntity);
            });
            return  widgeList;
        }
        return null;
    }

}